#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 15, INF = 0x3f3f3f3f;

int n;
int a[N], t1;
int b[N], t2;
int ans;

void dfs(int pos, int down, int up, int cnt)
{
    if (pos == 0) { ans = min(ans, cnt); return; }
    int l = down ? a[pos] : 0, r = up ? b[pos] : 9;
    if (l == r) {
        dfs(pos - 1, down && l == a[pos], up && l == b[pos], cnt + (l == a[pos]) + (l == b[pos]));
    } else if (r - l == 1) {
        dfs(pos - 1, down && l == a[pos], up && l == b[pos], cnt + (l == a[pos]) + (l == b[pos]));
        dfs(pos - 1, down && r == a[pos], up && r == b[pos], cnt + (r == a[pos]) + (r == b[pos]));
    } else {
        dfs(pos - 1, 0, 0, cnt);
    }
}

void solve()
{
    int x;cin >> x;
    t1 = 0;
    while (x) {
        a[++t1] = x % 10;
        x /= 10;
    }
    t2 = 0;
    cin >> x;
    while (x) {
        b[++t2] = x % 10;
        x /= 10;
    }
    n = t1;ans = INF;
    dfs(n, 1, 1, 0);
    cout << ans << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
    // #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"(14
1 1
2 3
4 6
15 16
17 19
199 201
899 999
1990 2001
6309 6409
12345 12501
19987 20093
746814 747932
900990999 900991010
999999999 999999999
)");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}